<?php
class AjaxController extends Zend_Controller_Action {
	
	public function init(){
		
	}
	
	public function refeshCodeAction() {
		$rand_string = rand(1000,9999);
		$authNamespace = new Zend_Session_Namespace('rancode');
		$authNamespace->code= $rand_string;
		$_SESSION['capcha']=$rand_string;
		echo '/capcha.php?v='.time();
		exit();
	}
    public function getManufactureAction(){
        $request=$this->getRequest();
        $type=$request->getParam('type');
        
        $mMotor=new Motor();
        $arrManufacture=$mMotor->getManufacture($type);
        $sFactory='<option value="">--Chọn thương hiệu--</option>';
        foreach($arrManufacture as $data){
            $this->view->NAME =$data['name'];
            $this->view->VALUE =$data['alias'];
            $sFactory.=$this->view->render('common-item/item_factory.phtml');
        }
        echo $sFactory;
        unset ($mMotor);
        exit;
    }
    public function getModelAction(){
        $request=$this->getRequest();
        $type=$request->type;
        $mMotor=new Motor();
        
        $alias = $request->manu_id;
        if($alias!=''&&$alias!='oto-khac'&&$alias!='xm-khac'){
            $sModel='<option value="">--Chọn model--</option>';
            
            if (intval ($alias)>0){                
                $arrModel=$mMotor->getModelByManuId(intval ($alias),'name','ASC'); 
                $sModel .= $this->writeoption ($arrModel);
            }else{
                $arrModel = $mMotor->getModelByManuAlias($alias,'name','ASC'); 
                foreach($arrModel as $data){
                    $this->view->NAME =$data['name'];
                    $this->view->VALUE =$data['alias'];
                    $sModel.=$this->view->render('common-item/item_model.phtml');
                }  
            }

            if ($type){
                $sModel .='<option value="model-oto-khac">Khác</option>';
            }else{
                $sModel .='<option value="model-xm-khac">Khác</option>';
            }
            
        }else{
            if($alias=='oto-khac')
            {
                $arrModel=$mMotor->getModel(1,'name','ASC');
                $sModel='<option value="">--Chọn model--</option>';
                foreach($arrModel as $data){
                    //if($data['alias']!='model-oto-khac')
//                    {
                    $this->view->NAME =$data['name'];
                    $this->view->VALUE =$data['alias'];
                    $sModel.=$this->view->render('common-item/item_model.phtml');
                    //}
                }
               // $sModel .='<option value="oto-khac">Khác</option>';
            }
            else if($alias=='xm-khac')
            {
                $arrModel=$mMotor->getModel(0,'name','ASC');
                $sModel='<option value="">--Chọn model--</option>';
                foreach($arrModel as $data){
                    //if($data['alias']!='model-xm-khac')
//                    {
                    $this->view->NAME =$data['name'];
                    $this->view->VALUE =$data['alias'];
                    $sModel.=$this->view->render('common-item/item_model.phtml');
                    //}
                }
                //$sModel .='<option value="xm-khac">Khác</option>';
            }
            else
            {
                $arrModel=$mMotor->getModel($type,'name','ASC');
                $sModel='<option value="">--Chọn model--</option>';
                foreach($arrModel as $data){
                    $this->view->NAME =$data['name'];
                    $this->view->VALUE =$data['alias'];
                    $sModel.=$this->view->render('common-item/item_model.phtml');
                }
            }
        }
        /*if($type==0)
        {
            $sModel.='<option value="model-xm-khac"> Khác </option>';
        }
        else if ($type==1)
        {
            $sModel.='<option value="model-oto-khac"> Khác </option>';
        }*/
        echo $sModel;               
        unset ($mMotor);        
        exit;
    }
    function getPriceAction(){
        $request=$this->getRequest();
        $type=$request->type;
        $mMotor=new Motor();
        $arrPrice=$mMotor->getPrice($type);
        $html = '<option value="">--Giá--</option>';
        foreach($arrPrice as $data){
            $this->view->NAME =$data['display'];
            $this->view->VALUE =$data['alias'];
            $html.=$this->view->render('common-item/item_price.phtml');
        }
        echo $html;
        unset ($mMotor);
        exit;
    }
    private function writeoption ($arrdata, $field_value='id', $field_name='name'){
        $html ='';
        foreach ($arrdata as $row){
            $html .= '<option value="'.$row[$field_value].'">'.$row[$field_name].'</option>';
        }
        return $html;
    }
}

?>